package com.itheima.controller;


import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Permission;
import com.itheima.service.CheckItemService;
import com.itheima.service.WpPermissionsService;

import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Param;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

/**
 * @Description:权限控制层
 * @Param:
 * @return:
 * @Author: 文攀
 * @Date: 2021/12/7
 */
@RestController
@RequestMapping("/wPermission")
public class WpPermissionsController {

    @Reference
    private WpPermissionsService wpPermissionsService;

    /**
     * @Description: 分页查询全部信息
     * @Param: queryPageBean
     * @return:
     * @Author: 文攀
     * @Date: 2021/12/7
     */
    @GetMapping("/findPage")
    public Result findPage(QueryPageBean queryPageBean) {
        try {
            PageResult pageResult = wpPermissionsService.findPage(queryPageBean);
            return new Result(true, "查询权限成功", pageResult);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "查询权限失败");
        }

    }

    /**
        * @Description: 新增权限项
        * @Param:
        * @return:
        * @Author: 文攀
        * @Date: 2021/12/7
        */
    @PostMapping("/add")
    public Result add(@RequestBody Permission permission) {
        try {
            wpPermissionsService.save(permission);
            return new Result(true, "添加权限项成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "添加权限项失败");
        }
    }

        /**
            * @Description:  根据id查询权限
            * @Param: id
            * @return:
            * @Author: 文攀
            * @Date: 2021/12/7
            */
    @GetMapping("/{id}")
    public Result findById(@PathVariable Integer id) {
        try {
            Permission byId = wpPermissionsService.findById(id);
            return new Result(true, "查询成功", byId);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "查询失败");
        }
    }

        /** 
            * @Description: 修改权限 
            * @Param:  
            * @return:  
            * @Author: 文攀
            * @Date: 2021/12/7 
            */
    @PutMapping("/edit")
    public Result edit(@RequestBody Permission permission){
        try {
            wpPermissionsService.edit(permission);
            return new Result(true,"修改权限成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"修改权限失败");
        }
    }


    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id ){
        try {
                /**
                    * @Description:  查询权限是否被引用，
                    * @Param: [id]
                    * @return:int
                    * @Author: 文攀
                    * @Date: 2021/12/8
                    */
            Integer count=wpPermissionsService.findPermissionReferencedCount(id);
            if (count>0){
                //权限被引用，不允许删除
                return new Result(false,"删除失败");
            }
            wpPermissionsService.delete(id);
            return new Result(true,"删除成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"删除失败");
        }
    }
}

